package com.machine.filling.function.main.cardInfoFg;

import android.content.Context;

import com.google.gson.Gson;
import com.machine.filling.Test.TestCommon;
import com.machine.filling.db.dao.CardTempDao;
import com.machine.filling.db.dao.ClientInfoDao;
import com.machine.filling.db.dao.GunInfoDao;
import com.machine.filling.net.http.network.APIHttpManager;
import com.machine.filling.net.http.network.BaseHttpStringCallback;
import com.machine.filling.net.serialPort.receive.Receive_PrepareFueling;
import com.machine.filling.net.serialPort.send.CMD_CardInfo;
import com.machine.filling.net.serialPort.util.SerialPortSendUtil;
import com.machine.filling.net.serialPort.util.SerialPortToolManager;
import com.machine.filling.net.ws.response.WsResCardInfoBean;
import com.machine.filling.util.Common;
import com.machine.filling.util.CustomToast;
import com.machine.filling.util.HexUtils;

import okhttp3.Call;

/**
 * Created by Administrator on 2018/3/2 0002.
 */

public class CardInfoPresenter implements CardInfoFgContract.Presenter {
    private Context mContext;
    private CardInfoFgContract.View mView;

    public CardInfoPresenter(Context mContext, CardInfoFgContract.View mView) {
        this.mContext = mContext;
        this.mView = mView;
    }

    @Override
    public void start() {

    }

    @Override
    public void distoryContext() {

    }

    @Override
    public void getCardInfo() {
        String gunId = mView.getGunId();
        String cardNum = mView.getCardNum();
        String encryCarNum = mView.getEncryCarNum();
        mView.showProgress(null);
        APIHttpManager.getInstance().getCardInfo(
                ClientInfoDao.getInstance().getModleByDeviceNum().getToken(),
                ClientInfoDao.getInstance().getModleByDeviceNum().getMachineId(),
                gunId, cardNum, encryCarNum, new BaseHttpStringCallback() {
            @Override
            public void onSuccess(Call call, String result) {
                WsResCardInfoBean bean = new Gson().fromJson(result,WsResCardInfoBean.class);
                mView.showCardInfoResult(bean);
            }

            @Override
            public void onFailed(String errorCode, String errorMsg, Exception exception) {
                mView.showCardInfoErrorResult(errorCode,errorMsg);
            }

            @Override
            public void onFinished() {
                mView.hideProgress();
            }
        });
    }

    /**
     * 弹卡,后台服务器找不到卡
     * @param portNum
     * @param cmd_prepare
     */
    @Override
    public void port_sendCardInfo_1101_errorCard(String portNum, Receive_PrepareFueling cmd_prepare) {
        CMD_CardInfo sendCardInfo = new CMD_CardInfo();
        sendCardInfo.setGunNum_2(HexUtils.str2ASCII(cmd_prepare.getGun_num_head_1(), 2));
        sendCardInfo.setPwdType_1("30");//无密码
        sendCardInfo.setPwd_6("303030303030");//密码
        sendCardInfo.setBindCarFlag_1("30");//车号标识
        sendCardInfo.setCarNum_6("303030303030");//车号
        sendCardInfo.setCarType_1("39");//数据库出错
        sendCardInfo.setDiscount_4("302E3030");
        sendCardInfo.setDepositBalance_10("303030303030302E3030");
        String frameStr = SerialPortSendUtil.getInstance().getCmd_SendCardInfo_1101(Common.MACHINE_VERSION,
                HexUtils.coverStrFromFirst(GunInfoDao.getInstance().queryBytPortNum(portNum).getGunNum(),2),
                sendCardInfo.getHexStr());
        SerialPortToolManager.getInstance().getSerialPortTool(portNum).sendSerialPort(frameStr);
    }
}
